const PipePoint = require('../models').PipePoint;
const OverfallCauseSub = require('../models').OverfallCauseSub;
const Op = require('../models').Sequelize.Op;

module.exports = {
    async listAll(req, res) {
        let para = {
            order: ['id'],
            limit: req.query.pageSize || 15,
            offset: req.query.curPage * req.query.pageSize || 0,
            order: [['id', 'desc']],            
        }
        if(req?.query?.name?.length>0){
            para.where = {
                name: {
                    [Op.like]: '%' + req.query.name + '%'
                }
            }
        }
        return await PipePoint.findAndCountAll(para).then(pipePoint => res.json(pipePoint))
    },

    async getById(req, res) {
        return await OverfallCause
            .findOne({
                where: { id: req.params.id },
                include: {
                    model: OverfallCauseSub,
                    as: 'overfall_has_subs'
                },
            })
            .then((overfallcause) => {
                if (!overfallcause) {
                    return res.status(404).send({
                        success: false,
                        message: 'OverfallCause Not Found',
                    });
                }
                return res.status(200).send(overfallcause);
            })
            .catch((error) => res.status(400).send(error));
    },

    async getByName(req, res) {
        await OverfallCause.findOne({
            where: {
                name: req.params.name
            }
        })
            .then((overfallcause) => {
                if (overfallcause) {
                    return res.status(200).send(overfallcause);
                }
            })
            .catch((error) => res.status(400).send(error));
    },

    async getByUser(req, res) {
        return await OverfallCause.findAndCountAll({
            where: {
                user: req.params.user
            }
        }).then(overfallcauses => res.json(overfallcauses))
    },

    async create(req, res) {    
        var coordinates = req.body.geo.match(/\d+\.\d+/g); // 提取经纬度信息
        var lat = parseFloat(coordinates[0]);
        var lng = parseFloat(coordinates[1]);

        let shape = '{"type":"Point","coordinates":['+lng+','+lat+']}'
        return PipePoint
            .create({
                name: req.body.name,
                geo: shape,                
                remark: req.body.remark,
            })
            .then((pipePoint) => res.status(201).send({
                success: true,
                id: pipePoint.id,
                message: 'add successful'
            }))
            .catch((error) => res.status(400).send(error));
    },

    async update(req, res) {
        var coordinates = req.body.geo.match(/\d+\.\d+/g); // 提取经纬度信息
        var lat = parseFloat(coordinates[0]);
        var lng = parseFloat(coordinates[1]);
        let shape = '{"type":"Point","coordinates":['+lng+','+lat+']}'
        return await PipePoint
            .findByPk(req.params.id)
            .then(pipePoint => {
                if (!pipePoint) {
                    return res.status(200).send({
                        success: false,
                        message: 'pipePoint Not Found',
                    });
                }
                return pipePoint
                    .update({
                        name: req.body.name || pipePoint.name,
                        geo: shape || pipePoint.geo,
                        remark: req.body.remark || pipePoint.remark                
                    })
                    .then(() => res.status(200).send(pipePoint))
                    .catch((error) => res.status(400).send(error));
            })
            .catch((error) => res.status(400).send(error));
    },

    async delete(req, res) {
        return await PipePoint
            .findByPk(req.params.id)
            .then(pipePoint => {
                if (!pipePoint) {
                    return res.status(200).send({
                        success: false,
                        message: 'PipePoint Not Found',
                    });
                } else {
                    return pipePoint
                        .destroy()
                        .then(() => res.status(200).send({
                            success: true,
                            message: 'delete successful'
                        }))
                        .catch((error) => res.status(400).send(error));
                }
            })
            .catch((error) => res.status(400).send(error));
    },
}